package com.chalk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.dao.CoachDao;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.*;
import com.chalk.service.*;
import com.chalk.util.MpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-21
 */

@Service
public class CoachServiceImpl extends ServiceImpl<CoachDao, Coach> implements CoachService {

    @Autowired
    MpUtil<Coach> mpUtil;

    @Autowired
    CoachAndSeService coachAndSeService;
    @Autowired
    CoachDao coachDao;

    @Autowired
    GradeService gradeService;

    @Autowired
    OrgService orgService;

    @Autowired
    SportEventService sportEventService;

    @Autowired
    CoachService coachService;

    @Override
    public Page<Coach> getCoachForPage(Map<String, Object> map) throws ColumnNotEmptyException {
        EntityWrapper<Coach> wrapper = mpUtil.getWrapper();
        Page<Coach> coachPage = mpUtil.getPage(map);
        coachPage.setDescs(mpUtil.getDesc("create_time"));
        Page<Coach> coachPage1 = selectPage(coachPage, wrapper);

        for (int i = 0; i < coachPage1.getRecords().size(); i++) {
            //教练运动等级
            Integer gradeId = coachPage1.getRecords().get(i).getGradeId();
            Grade grade = gradeService.selectById(gradeId);
            coachPage1.getRecords().get(i).setGradeTitle(grade.getGradeTitle());
            coachPage1.getRecords().get(i).setGradeLable(grade.getGradeLable());
            //教练运动场馆
            Integer orgId = coachPage1.getRecords().get(i).getOrgId();
            Org org = orgService.selectById(orgId);
            coachPage1.getRecords().get(i).setOrgName(org.getOrgName());

            //教练运动项目
            List<SportEvent> sportEvents = sportEventService.selectSportEventBySeId(coachPage1.getRecords().get(i).getCoachId());
            coachPage1.getRecords().get(i).setSeName(sportEvents);
        }
        return coachPage1;
    }

    @Override
    @Transactional
    public int deleteBatchByPrimaryKey(String[] ids) {
        return coachDao.deleteBatchByPrimaryKey(ids);
    }


    @Override
    @Transactional
    public boolean addCoach(Coach entity) {
        boolean f = coachService.insert(entity);
        System.out.println("Coach对象为：" + entity.toString());
        if(f){
            for (int i = 0; i < entity.getSportEvents().size(); i++) {
                coachAndSeService.insert(new CoachAndSe(entity.getCoachId(),entity.getSportEvents().get(i)));
            }
        }
        return f;
    }

    @Override
    public int updateCoach(Coach entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteCoach(Integer id) {
        return baseMapper.deleteById(id);
    }

}
